<!DOCTYPE html>
<!-- saved from url=(0044)https://jenkins.io/doc/book/pipeline/docker/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
Using Docker with Pipeline
</title>

<meta content="Jenkins is an open source automation server" name="description">
<link href="https://jenkins.io/sites/default/files/jenkins_favicon.ico" rel="shortcut icon" type="image/x-icon">

<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="ie=edge" http-equiv="x-ua-compatible">
<link href="https://jenkins.io/img/favicon.ico" rel="icon" sizes="32x32" type="img/png">
<link href="https://jenkins.io/img/favicon-32x32.png" rel="icon" sizes="32x32" type="image/png">
<link href="https://jenkins.io/img/favicon-16x16.png" rel="icon" sizes="16x16" type="image/png">
<link href="https://jenkins.io/img/apple-touch-icon-76x76.png" rel="apple-touch-icon" sizes="76x76">
<link href="https://jenkins.io/img/apple-touch-icon-120x120.png" rel="apple-touch-icon" sizes="120x120">
<link href="https://jenkins.io/img/apple-touch-icon-152x152.png" rel="apple-touch-icon" sizes="152x152">
<meta content="Using Docker with Pipeline" name="apple-mobile-web-app-title">
<!-- Twitter Card data -->
<meta content="summary_large_image" name="twitter:card">
<meta content="@JenkinsCI" name="twitter:site">
<meta content="Using Docker with Pipeline" name="twitter:title">
<meta content="Jenkins – an open source automation server which enables developers around the world to reliably build, test, and deploy their software." name="twitter:description">
<meta content="@JenkinsCI" name="twitter:creator">
<!-- Twitter Summary card images must be at least 120x120px -->
<!-- Open Graph data -->
<meta content="Using Docker with Pipeline" property="og:title">
<meta content="article" property="og:type">
<meta content="https://jenkins.io/doc/book/pipeline/docker/index.html" property="og:url">
<meta content="Jenkins – an open source automation server which enables developers around the world to reliably build, test, and deploy their software." name="og:description">
<meta content="Using Docker with Pipeline" property="og:site_name">
<style class="anchorjs"></style><link href="./Using Docker with Pipeline_files/bootstrap.min.css" media="screen" rel="stylesheet">
<link href="./Using Docker with Pipeline_files/tether.min.css" media="screen" rel="stylesheet">
<link href="./Using Docker with Pipeline_files/font-icons.css" media="screen" rel="stylesheet">
<link href="./Using Docker with Pipeline_files/jenkins.css" media="screen" rel="stylesheet">
<!-- Non-obtrusive CSS styles -->
<link href="./Using Docker with Pipeline_files/ionicons.min.css" media="screen" rel="stylesheet">
<link href="./Using Docker with Pipeline_files/footer.css" media="screen" rel="stylesheet">
<link href="./Using Docker with Pipeline_files/font-awesome.min.css" media="screen" rel="stylesheet">
</head>
<body>
<script id="twitter-wjs" src="./Using Docker with Pipeline_files/widgets.js.下载"></script><script async="" src="./Using Docker with Pipeline_files/analytics.js.下载"></script><script src="./Using Docker with Pipeline_files/jquery.js.下载"></script>
<!-- starting partial toptoolbar.html.haml -->
<nav class="navbar navbar-toggleable-md navbar-inverse top bg-inverse fixed-top" id="ji-toolbar">
<div class="container">
<button class="navbar-toggler navbar-toggler-rght hidden-lg-up float-right" data-target="#CollapsingNavbar" data-toggle="collapse" type="button">
<i class="ion-navicon"></i>
</button>
<a class="navbar-brand" href="https://jenkins.io/">
Jenkins
</a>
<div class="collapse navbar-collapse" id="CollapsingNavbar">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="https://jenkins.io/node">
Blog
</a>
</li>
<li class="nav-item dropdown">
<div aria-expanded="false" aria-haspopup="true" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button">
Documentation
</div>
<div class="dropdown-menu">
<a class="dropdown-item" href="https://jenkins.io/doc">
Use Jenkins
</a>
<a class="dropdown-item" href="https://jenkins.io/doc/developer">
Extend Jenkins
</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="https://plugins.jenkins.io/">
Plugins
</a>
</li>
<li class="nav-item dropdown">
<div aria-expanded="false" aria-haspopup="true" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button">
Use-cases
</div>
<div class="dropdown-menu">
<a class="dropdown-item feature" href="https://jenkins.io/solutions/android">
Android
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/c">
C/C++
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/docker">
Docker
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/embedded">
Embedded
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/github">
GitHub
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/java">
Java
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/php">
PHP
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/pipeline">
Continuous Delivery
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/python">
Python
</a>
<a class="dropdown-item feature" href="https://jenkins.io/solutions/ruby">
Ruby
</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="https://jenkins.io/participate">
Participate
</a>
</li>
<li class="dropdown nav-item">
<div aria-expanded="false" aria-haspopup="true" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button">Sub-projects</div>
<div class="dropdown-menu">
<a class="dropdown-item feature" href="https://jenkins.io/projects/">
Overview
</a>
<a class="dropdown-item feature" href="https://jenkins.io/projects/blueocean/">
Blue Ocean
</a>
<a class="dropdown-item feature" href="https://jenkins.io/projects/gsoc/">
Google Summer of Code
</a>
<a class="dropdown-item feature" href="https://jenkins.io/projects/infrastructure/">
Infrastructure
</a>
<a class="dropdown-item feature" href="https://jenkins.io/projects/jam/">
Jenkins Area Meetups
</a>
<a class="dropdown-item feature" href="https://jenkins.io/projects/remoting/">
Jenkins Remoting
</a>
</div>
</li>
<li class="nav-item dropdown">
<div aria-expanded="false" aria-haspopup="true" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button">
Resources
</div>
<div class="dropdown-menu">
<a class="dropdown-item feature" href="https://accounts.jenkins.io/" title="Create/manage your account for accessing wiki, issue tracker, etc">
Account Management
</a>
<a class="dropdown-item" href="https://jenkins.io/chat" title="Chat with the rest of the Jenkins community on IRC">
Chat
</a>
<a class="dropdown-item feature" href="https://issues.jenkins-ci.org/">
Issue Tracker
</a>
<a class="dropdown-item" href="https://jenkins.io/mailing-lists" title="Browse Jenkins mailing list archives and/or subscribe to lists">
Mailing Lists
</a>
<a class="dropdown-item feature" href="https://jenkins.io/events">
Events
</a>
<a class="dropdown-item feature" href="https://wiki.jenkins-ci.org/">
Wiki
</a>
</div>
</li>
<li class="nav-item dropdown">
<div aria-expanded="false" aria-haspopup="true" class="nav-link dropdown-toggle" data-toggle="dropdown" role="button">
About
</div>
<div class="dropdown-menu">
<a class="dropdown-item" href="https://jenkins.io/security">
Security
</a>
<a class="dropdown-item" href="https://jenkins.io/press">
Press
</a>
<a class="dropdown-item" href="https://jenkins.io/awards">
Awards
</a>
<a class="dropdown-item" href="https://jenkins.io/project/conduct">
Conduct
</a>
<a class="dropdown-item" href="https://jenkins.io/artwork">
Artwork
</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link btn btn-outline-secondary" href="https://jenkins.io/download">
Download
</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Spacing to make the fixed-top sticky navbar not occlude any content below it -->
<div class="pt-4">
&nbsp;
</div>

<!-- ending partial toptoolbar.html.haml -->
<script>
  $(function () {
    for (var i = 1 ; i <= 6 ; i ++) {
      anchors.add('.container .row .col-lg-9 h' + i);
    }
  })
</script>
<div class="container">
<div class="row body">
<div class="col-lg-3">
<div class="sidebar-nav tour">
<p>
<a href="https://jenkins.io/doc" class="">Jenkins User Documentation Home</a>
</p>
<h4>
Guided Tour
</h4>
<ul>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/getting-started" class="">Getting started</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/hello-world" class="">Creating your first Pipeline</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/running-multiple-steps" class="">Running multiple steps</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/agents" class="">Defining execution environments</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/environment" class="">Using environment variables</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/tests-and-artifacts" class="">Recording test results and artifacts</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/post" class="">Cleaning up and notifications</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/tour/deployment" class="">Deployment</a>
</li>
</ul>
<h4>
Tutorials
</h4>
<ul>
<li>
<a href="https://jenkins.io/doc/tutorials" class="">Overview</a>
</li>
<li>
<a href="https://jenkins.io/doc/tutorials/build-a-java-app-with-maven" class="">Build a Java app with Maven</a>
</li>
<li>
<a href="https://jenkins.io/doc/tutorials/build-a-node-js-and-react-app-with-npm" class="">Build a Node.js and React app with npm</a>
</li>
<li>
<a href="https://jenkins.io/doc/tutorials/build-a-python-app-with-pyinstaller" class="">Build a Python app with PyInstaller</a>
</li>
<li>
<a href="https://jenkins.io/doc/tutorials/create-a-pipeline-in-blue-ocean" class="">Create a Pipeline in Blue Ocean</a>
</li>
<li>
<a href="https://jenkins.io/doc/tutorials/build-a-multibranch-pipeline-project" class="">Build a multibranch Pipeline project</a>
</li>
</ul>
<h4>
User Handbook
<span style="font-size: 0.6rem; vertical-align: middle;">
<a href="https://jenkins.io/user-handbook.pdf">
(PDF)
</a>
</span>
</h4>
<ul>
<li>
<a href="https://jenkins.io/doc/book/getting-started" class="">User Handbook overview</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/installing" class="">Installing Jenkins</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/using" class="">Using Jenkins</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline" class="active">Pipeline</a>
<small>
<ul style="padding-left: 1rem;">
<li>
<a href="https://jenkins.io/doc/book/pipeline/getting-started" class="">Getting started with Pipeline</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/jenkinsfile" class="">Using a Jenkinsfile</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/multibranch" class="">Branches and Pull Requests</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/docker" class="active">Using Docker with Pipeline</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/shared-libraries" class="">Extending with Shared Libraries</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/development" class="">Pipeline Development Tools</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/syntax" class="">Pipeline Syntax</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/pipeline/scaling-pipeline" class="">Scaling Pipelines</a>
</li>
</ul>
</small>
</li>
<li>
<a href="https://jenkins.io/doc/book/blueocean" class="">Blue Ocean</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/managing" class="">Managing Jenkins</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/system-administration" class="">System Administration</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/scaling" class="">Scaling Jenkins</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/appendix" class="">Appendix</a>
</li>
<li>
<a href="https://jenkins.io/doc/book/glossary" class="">Glossary</a>
</li>
</ul>
<h4>
Resources
</h4>
<ul>
<li>
<a href="https://jenkins.io/doc/book/pipeline/syntax" class="">Pipeline Syntax reference</a>
</li>
<li>
<a href="https://jenkins.io/doc/pipeline/steps" class="">Pipeline Steps reference</a>
</li>
<li>
<a href="https://jenkins.io/doc/upgrade-guide" class="">LTS Upgrade Guide</a>
</li>
</ul>
<h5>
Recent Tutorial Blog Posts
</h5>
<ul>
<li>
<a href="https://jenkins.io/blog/2017/11/27/tutorials-in-the-jenkins-user-documentation/">
Introducing Tutorials in the Jenkins User Documentation
</a>
</li>
<li>
<a href="https://jenkins.io/blog/2017/05/18/pipeline-dev-tools/">
Pipeline Development Tools
</a>
</li>
<li>
<a href="https://jenkins.io/blog/2017/04/12/welcome-to-blue-ocean-dashboard/">
Getting Started with the Blue Ocean Dashboard
</a>
</li>
</ul>
<div class="small">
<a href="https://jenkins.io/node/tags/tutorial">
View all tutorial blog posts
</a>
</div>
<p></p>
</div>
</div>
<div class="col-lg-9">
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="row">
<a class="prev doc-page-link" href="https://jenkins.io/doc/book/pipeline/multibranch">
⇐ Branches and Pull Requests
</a>
</div>
</div>
<div class="col-md-4">
<div class="row">
<a class="up doc-page-link" href="https://jenkins.io/doc/book/pipeline/">
⇑ Pipeline
</a>
</div>
<div class="row">
<a class="index doc-page-link" href="https://jenkins.io/doc/book/">
Index
</a>
</div>
</div>
<div class="col-md-4">
<div class="row">
<a class="next doc-page-link" href="https://jenkins.io/doc/book/pipeline/shared-libraries">
Extending with Shared Libraries ⇒
</a>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row body">
<div class="section">
<h1 id="using-docker-with-pipeline">
Using Docker with Pipeline
<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#using-docker-with-pipeline" aria-label="Anchor link for: using docker with pipeline" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h1>
<div id="toc" class="toc">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#execution-environment">Customizing the execution environment</a>
<ul class="sectlevel2">
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#caching-data-for-containers">Caching data for containers</a></li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#using-multiple-containers">Using multiple containers</a></li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#dockerfile">Using a Dockerfile</a></li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#specifying-a-docker-label">Specifying a Docker Label</a></li>
</ul>
</li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#advanced-usage-with-scripted-pipeline">Advanced Usage with Scripted Pipeline</a>
<ul class="sectlevel2">
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers">Running "sidecar" containers</a></li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#building-containers">Building containers</a></li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#using-a-remote-docker-server">Using a remote Docker server</a></li>
<li><a href="https://jenkins.io/doc/book/pipeline/docker/#custom-registry">Using a custom registry</a></li>
</ul>
</li>
</ul>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Many organizations use <a href="https://www.docker.com/">Docker</a> to unify their build
and test environments across machines, and to provide an efficient mechanism
for deploying applications. Starting with Pipeline versions 2.5 and higher,
Pipeline has built-in support for interacting with Docker from within a
<code>Jenkinsfile</code>.</p>
</div>
<div class="paragraph">
<p>While this section will cover the basics of utilizing Docker from with a
<code>Jenkinsfile</code>, it will not cover the fundamentals of Docker, which can be read
about in the
<a href="https://docs.docker.com/get-started/">Docker Getting Started Guide</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="execution-environment"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#execution-environment"></a>Customizing the execution environment<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#execution-environment" aria-label="Anchor link for: execution environment" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Pipeline is designed to easily use
<a href="https://docs.docker.com/">Docker</a>
images as the execution environment for a single
<a href="https://jenkins.io/doc/book/glossary/#stage">Stage</a>
or the entire Pipeline. Meaning that a user can define the tools required for
their Pipeline, without having to manually configure agents.
Practically any tool which can be
<a href="http://hub.docker.com/">packaged in a Docker container</a>.
can be used with ease by making only minor edits to a <code>Jenkinsfile</code>.</p>
</div>
<div class="pipeline-block">  <div class="listingblock pipeline-declarative">
    <div class="title">Jenkinsfile (Declarative Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">pipeline {
    agent {
        docker { image <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node:7-alpine</span><span style="color:#710">'</span></span> }
    }
    stages {
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Test</span><span style="color:#710">'</span></span>) {
            steps {
                sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node --version</span><span style="color:#710">'</span></span>
            }
        }
    }
}</code></pre>
</div></div>  <div class="pipeline-script-expand">
    <a href="https://jenkins.io/doc/book/pipeline/docker/#" onclick="javascript:$(this).parent().siblings(&#39;.pipeline-script&#39;).toggle(); return false;">Toggle Scripted Pipeline</a>
    <em>(Advanced)</em>
  </div>
  <div class="listingblock pipeline-script" style="display: none">
    <div class="title">Jenkinsfile (Scripted Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">node {
    <span style="color:#777">/* Requires the Docker Pipeline plugin to be installed */</span>
    docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node:7-alpine</span><span style="color:#710">'</span></span>).inside {
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Test</span><span style="color:#710">'</span></span>) {
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node --version</span><span style="color:#710">'</span></span>
        }
    }
}</code></pre>
</div></div></div>
<div class="paragraph">
<p>When the Pipeline executes, Jenkins will automatically start the specified
container and execute the defined steps within it:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code>[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
[guided-tour] Running shell script
+ node --version
v7.4.0
[Pipeline] }
[Pipeline] // stage
[Pipeline] }</code></pre>
</div>
</div>
<div class="sect2">
<h3 id="caching-data-for-containers"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#caching-data-for-containers"></a>Caching data for containers<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#caching-data-for-containers" aria-label="Anchor link for: caching data for containers" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>Many build tools will download external dependencies and cache them locally for
future re-use. Since containers are initially created with "clean" file
systems, this can result in slower Pipelines, as they may not take advantage of
on-disk caches between subsequent Pipeline runs.</p>
</div>
<div class="paragraph">
<p>Pipeline supports adding custom arguments which are passed
to Docker, allowing users to specify custom
<a href="https://docs.docker.com/engine/tutorials/dockervolumes/">Docker Volumes</a>
to mount, which can be used for caching data on the
<a href="https://jenkins.io/doc/book/glossary/#agent">agent</a>
between Pipeline runs. The following example will cache <code>~/.m2</code> between
Pipeline runs utilizing the
<a href="https://hub.docker.com/_/maven/"><code>maven</code> container</a>,
 thereby avoiding the need to re-download dependencies for subsequent runs of
 the Pipeline.</p>
</div>
<div class="pipeline-block">  <div class="listingblock pipeline-declarative">
    <div class="title">Jenkinsfile (Declarative Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">pipeline {
    agent {
        docker {
            image <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">maven:3-alpine</span><span style="color:#710">'</span></span>
            args <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">-v $HOME/.m2:/root/.m2</span><span style="color:#710">'</span></span>
        }
    }
    stages {
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Build</span><span style="color:#710">'</span></span>) {
            steps {
                sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mvn -B</span><span style="color:#710">'</span></span>
            }
        }
    }
}</code></pre>
</div></div>  <div class="pipeline-script-expand">
    <a href="https://jenkins.io/doc/book/pipeline/docker/#" onclick="javascript:$(this).parent().siblings(&#39;.pipeline-script&#39;).toggle(); return false;">Toggle Scripted Pipeline</a>
    <em>(Advanced)</em>
  </div>
  <div class="listingblock pipeline-script" style="display: none">
    <div class="title">Jenkinsfile (Scripted Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">node {
    <span style="color:#777">/* Requires the Docker Pipeline plugin to be installed */</span>
    docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">maven:3-alpine</span><span style="color:#710">'</span></span>).inside(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">-v $HOME/.m2:/root/.m2</span><span style="color:#710">'</span></span>) {
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Build</span><span style="color:#710">'</span></span>) {
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mvn -B</span><span style="color:#710">'</span></span>
        }
    }
}</code></pre>
</div></div></div>
</div>
<div class="sect2">
<h3 id="using-multiple-containers"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#using-multiple-containers"></a>Using multiple containers<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#using-multiple-containers" aria-label="Anchor link for: using multiple containers" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>It has become increasingly common for code bases to rely on
multiple, different, technologies. For example, a repository might have both a
Java-based back-end API implementation <em>and</em> a JavaScript-based front-end
implementation. Combining Docker and Pipeline allows a <code>Jenkinsfile</code> to use
<strong>multiple</strong> types of technologies by combining the <code>agent {}</code> directive, with
different stages.</p>
</div>
<div class="pipeline-block">  <div class="listingblock pipeline-declarative">
    <div class="title">Jenkinsfile (Declarative Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">pipeline {
    agent none
    stages {
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Back-end</span><span style="color:#710">'</span></span>) {
            agent {
                docker { image <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">maven:3-alpine</span><span style="color:#710">'</span></span> }
            }
            steps {
                sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mvn --version</span><span style="color:#710">'</span></span>
            }
        }
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Front-end</span><span style="color:#710">'</span></span>) {
            agent {
                docker { image <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node:7-alpine</span><span style="color:#710">'</span></span> }
            }
            steps {
                sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node --version</span><span style="color:#710">'</span></span>
            }
        }
    }
}</code></pre>
</div></div>  <div class="pipeline-script-expand">
    <a href="https://jenkins.io/doc/book/pipeline/docker/#" onclick="javascript:$(this).parent().siblings(&#39;.pipeline-script&#39;).toggle(); return false;">Toggle Scripted Pipeline</a>
    <em>(Advanced)</em>
  </div>
  <div class="listingblock pipeline-script" style="display: none">
    <div class="title">Jenkinsfile (Scripted Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">node {
    <span style="color:#777">/* Requires the Docker Pipeline plugin to be installed */</span>

    stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Back-end</span><span style="color:#710">'</span></span>) {
        docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">maven:3-alpine</span><span style="color:#710">'</span></span>).inside {
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mvn --version</span><span style="color:#710">'</span></span>
        }
    }

    stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Front-end</span><span style="color:#710">'</span></span>) {
        docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node:7-alpine</span><span style="color:#710">'</span></span>).inside {
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node --version</span><span style="color:#710">'</span></span>
        }
    }
}</code></pre>
</div></div></div>
</div>
<div class="sect2">
<h3 id="dockerfile"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#dockerfile"></a>Using a Dockerfile<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#dockerfile" aria-label="Anchor link for: dockerfile" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>For projects which require a more customized execution environment, Pipeline
also supports building and running a container from a <code>Dockerfile</code> in the source
repository. In contrast to the <a href="https://jenkins.io/doc/book/pipeline/docker/#execution-environment">previous approach</a> of using
an "off-the-shelf" container, using the <code>agent { dockerfile true }</code> syntax will
build a new image from a <code>Dockerfile</code> rather than pulling one from
<a href="https://hub.docker.com/">Docker Hub</a>.</p>
</div>
<div class="paragraph">
<p>Re-using an example from above, with a more custom <code>Dockerfile</code>:</p>
</div>
<div class="listingblock">
<div class="title">Dockerfile</div>
<div class="content">
<pre class="CodeRay highlight nowrap"><code>FROM node:7-alpine

RUN apk add -U subversion</code></pre>
</div>
</div>
<div class="paragraph">
<p>By committing this to the root of the source repository, the <code>Jenkinsfile</code> can
be changed to build a container based on this <code>Dockerfile</code> and then run the
defined steps using that container:</p>
</div>
<div class="pipeline-block">  <div class="listingblock pipeline-declarative">
    <div class="title">Jenkinsfile (Declarative Pipeline)</div>
    <div class="content">
  <pre class="CodeRay highlight nowrap"><code class="language-groovy" data-lang="groovy">pipeline {
    agent { dockerfile <span style="color:#069">true</span> }
    stages {
        stage(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Test</span><span style="color:#710">'</span></span>) {
            steps {
                sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">node --version</span><span style="color:#710">'</span></span>
                sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">svn --version</span><span style="color:#710">'</span></span>
            }
        }
    }
}</code></pre>
</div></div></div>
<div class="paragraph">
<p>The <code>agent { dockerfile true }</code> syntax supports a number of other options which
are described in more detail in the
<a href="https://jenkins.io/doc/book/pipeline/syntax#agent">Pipeline Syntax</a> section.</p>
</div>
<div class="videoblock">
<div class="title">Using a Dockerfile with Jenkins Pipeline</div>
<div class="content">
<iframe width="852" height="480" src="./Using Docker with Pipeline_files/Pi2kJ2RJS50.html" frameborder="0" allowfullscreen=""></iframe>
</div>
</div>
</div>
<div class="sect2">
<h3 id="specifying-a-docker-label"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#specifying-a-docker-label"></a>Specifying a Docker Label<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#specifying-a-docker-label" aria-label="Anchor link for: specifying a docker label" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>By default, Pipeline assumes that <em>any</em> configured
<a href="https://jenkins.io/doc/book/glossary/#agent">agent</a> is capable of running Docker-based Pipelines.
For Jenkins environments which have macOS, Windows, or other agents, which are
unable to run the Docker daemon, this default setting may be problematic.
Pipeline provides a global option in the <strong>Manage Jenkins</strong> page, and on
the
<a href="https://jenkins.io/doc/book/glossary/#folder">Folder</a>
level, for specifying which agents (by
<a href="https://jenkins.io/doc/book/glossary/#label">Label</a>)
to use for running Docker-based Pipelines.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="./Using Docker with Pipeline_files/configure-docker-label.png" alt="Configuring the Pipeline Docker Label">
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="advanced-usage-with-scripted-pipeline"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#advanced-usage-with-scripted-pipeline"></a>Advanced Usage with Scripted Pipeline<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#advanced-usage-with-scripted-pipeline" aria-label="Anchor link for: advanced usage with scripted pipeline" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="running-sidecar-containers"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers"></a>Running "sidecar" containers<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#running-sidecar-containers" aria-label="Anchor link for: running sidecar containers" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>Using Docker in Pipeline can be an effective way to run a service on which the
build, or a set of tests, may rely. Similar to the
<a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar">sidecar
pattern</a>, Docker Pipeline can run one container "in the background", while
performing work in another. Utilizing this sidecar approach, a Pipeline can
have a "clean" container provisioned for each Pipeline run.</p>
</div>
<div class="paragraph">
<p>Consider a hypothetical integration test suite which relies on a local MySQL
database to be running. Using the <code>withRun</code> method, implemented in the
<a href="https://plugins.jenkins.io/docker-workflow">Docker Pipeline</a> plugin’s support for Scripted Pipeline,
a <code>Jenkinsfile</code> can run MySQL as a sidecar:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm
    <span style="color:#777">/*
     * In order to communicate with the MySQL server, this Pipeline explicitly
     * maps the port (`3306`) to a known port on the host machine.
     */</span>
    docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mysql:5</span><span style="color:#710">'</span></span>).withRun(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">-e "MYSQL_ROOT_PASSWORD=my-secret-pw" -p 3306:3306</span><span style="color:#710">'</span></span>) { c -&gt;
        <span style="color:#777">/* Wait until mysql service is up */</span>
        sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">while ! mysqladmin ping -h0.0.0.0 --silent; do sleep 1; done</span><span style="color:#710">'</span></span>
        <span style="color:#777">/* Run some tests which require MySQL */</span>
        sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">make check</span><span style="color:#710">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This example can be taken further, utilizing two containers simultaneously.
One "sidecar" running MySQL, and another providing the <a href="https://jenkins.io/doc/book/pipeline/docker/#execution-environment">execution environment</a>, by using the Docker
<a href="https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/">container links</a>.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm
    docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mysql:5</span><span style="color:#710">'</span></span>).withRun(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">-e "MYSQL_ROOT_PASSWORD=my-secret-pw"</span><span style="color:#710">'</span></span>) { c -&gt;
        docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mysql:5</span><span style="color:#710">'</span></span>).inside(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">--link </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>c.id<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">:db</span><span style="color:#710">"</span></span>) {
            <span style="color:#777">/* Wait until mysql service is up */</span>
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">while ! mysqladmin ping -hdb --silent; do sleep 1; done</span><span style="color:#710">'</span></span>
        }
        docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">centos:7</span><span style="color:#710">'</span></span>).inside(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">--link </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>c.id<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20">:db</span><span style="color:#710">"</span></span>) {
            <span style="color:#777">/*
             * Run some tests which require MySQL, and assume that it is
             * available on the host name `db`
             */</span>
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">make check</span><span style="color:#710">'</span></span>
        }
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The above example uses the object exposed by <code>withRun</code>, which has the
running container’s ID available via the <code>id</code> property. Using the container’s
ID, the Pipeline can create a link by passing custom Docker arguments to the
<code>inside()</code> method.</p>
</div>
<div class="paragraph">
<p>The <code>id</code> property can also be useful for inspecting logs from a running Docker
container before the Pipeline exits:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">docker logs </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>c.id<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span></code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="building-containers"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#building-containers"></a>Building containers<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#building-containers" aria-label="Anchor link for: building containers" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>In order to create a Docker image, the <a href="https://plugins.jenkins.io/docker-workflow">Docker Pipeline</a>
plugin also provides a <code>build()</code> method for creating a new image, from a
<code>Dockerfile</code> in the repository, during a Pipeline run.</p>
</div>
<div class="paragraph">
<p>One major benefit of using the syntax <code>docker.build("my-image-name")</code> is that a
Scripted Pipeline can use the return value for subsequent Docker Pipeline
calls, for example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm

    <span style="color:#080;font-weight:bold">def</span> customImage = docker.build(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">my-image:</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>env.BUILD_ID<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>)

    customImage.inside {
        sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">make test</span><span style="color:#710">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The return value can also be used to publish the Docker image to
<a href="https://hub.docker.com/">Docker Hub</a>,
or a <a href="https://jenkins.io/doc/book/pipeline/docker/#custom-registry">custom Registry</a>,
via the <code>push()</code> method, for example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm
    <span style="color:#080;font-weight:bold">def</span> customImage = docker.build(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">my-image:</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>env.BUILD_ID<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>)
    customImage.push()
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>One common usage of image "tags" is to specify a <code>latest</code> tag for the most
recently, validated, version of a Docker image. The <code>push()</code> method accepts an
optional <code>tag</code> parameter, allowing the Pipeline to push the <code>customImage</code> with
different tags, for example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm
    <span style="color:#080;font-weight:bold">def</span> customImage = docker.build(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">my-image:</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>env.BUILD_ID<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>)
    customImage.push()

    customImage.push(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">latest</span><span style="color:#710">'</span></span>)
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The <code>build()</code> method builds the <code>Dockerfile</code> in the current directory by
default. This can be overridden by providing a directory path
containing a <code>Dockerfile</code> as the second argument of the <code>build()</code> method, for example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm
    <span style="color:#080;font-weight:bold">def</span> testImage = docker.build(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">test-image</span><span style="color:#710">"</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">./dockerfiles/test</span><span style="color:#710">"</span></span>) <i class="conum" data-value="1"></i><b>(1)</b>

    testImage.inside {
        sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">make test</span><span style="color:#710">'</span></span>
    }
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tbody><tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Builds <code>test-image</code> from the Dockerfile found at <code>./dockerfiles/test/Dockerfile</code>.</td>
</tr>
</tbody></table>
</div>
<div class="paragraph">
<p>It is possible to pass other arguments to
<a href="https://docs.docker.com/engine/reference/commandline/build/">docker build</a>
by adding them to the second argument of the <code>build()</code> method.
When passing arguments this way, the last value in the that string must be
the path to the docker file.</p>
</div>
<div class="paragraph">
<p>This example overrides the default <code>Dockerfile</code> by passing the <code>-f</code>
flag:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm
    <span style="color:#080;font-weight:bold">def</span> dockerfile = <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">Dockerfile.test</span><span style="color:#710">'</span></span>
    <span style="color:#080;font-weight:bold">def</span> customImage = docker.build(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">my-image:</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>env.BUILD_ID<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">-f </span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>dockerfile<span style="font-weight:bold;color:#666">}</span></span><span style="color:#D20"> ./dockerfiles</span><span style="color:#710">"</span></span>) <i class="conum" data-value="1"></i><b>(1)</b>
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tbody><tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Builds <code>my-image:${env.BUILD_ID}</code> from the Dockerfile found at <code>./dockerfiles/Dockerfile.test</code>.</td>
</tr>
</tbody></table>
</div>
</div>
<div class="sect2">
<h3 id="using-a-remote-docker-server"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#using-a-remote-docker-server"></a>Using a remote Docker server<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#using-a-remote-docker-server" aria-label="Anchor link for: using a remote docker server" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>By default, the <a href="https://plugins.jenkins.io/docker-workflow">Docker Pipeline</a> plugin will communicate
with a local Docker daemon, typically accessed through <code>/var/run/docker.sock</code>.</p>
</div>
<div class="paragraph">
<p>To select a non-default Docker server, such as with
<a href="https://docs.docker.com/swarm/">Docker Swarm</a>,
the <code>withServer()</code> method should be used.</p>
</div>
<div class="paragraph">
<p>By passing a URI, and optionally the Credentials ID of a <strong>Docker Server
Certificate Authentication</strong> pre-configured in Jenkins, to the method with:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm

    docker.withServer(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">tcp://swarm.example.com:2376</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">swarm-certs</span><span style="color:#710">'</span></span>) {
        docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">mysql:5</span><span style="color:#710">'</span></span>).withRun(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">-p 3306:3306</span><span style="color:#710">'</span></span>) {
            <span style="color:#777">/* do things */</span>
        }
    }
}</code></pre>
</div>
</div>
<div class="admonitionblock caution">
<table>
<tbody><tr>
<td class="icon">
<i class="fa icon-caution" title="Caution"></i>
</td>
<td class="content">
<div class="paragraph">
<p><code>inside()</code> and <code>build()</code> will not work properly with a Docker Swarm server out
of the box</p>
</div>
<div class="paragraph">
<p>For <code>inside()</code> to work, the Docker server and the Jenkins agent must use the
same filesystem, so that the workspace can be mounted.</p>
</div>
<div class="paragraph">
<p>Currently neither the Jenkins plugin nor the Docker CLI will automatically
detect the case that the server is running remotely; a typical symptom would be
errors from nested <code>sh</code> commands such as</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code>cannot create /…@tmp/durable-…/pid: Directory nonexistent</code></pre>
</div>
</div>
<div class="paragraph">
<p>When Jenkins detects that the agent is itself running inside a Docker
container, it will automatically pass the <code>--volumes-from</code> argument to the
<code>inside</code> container, ensuring that it can share a workspace with the agent.</p>
</div>
<div class="paragraph">
<p>Additionally some versions of Docker Swarm do not support custom Registries.</p>
</div>
</td>
</tr>
</tbody></table>
</div>
</div>
<div class="sect2">
<h3 id="custom-registry"><a class="anchor" href="https://jenkins.io/doc/book/pipeline/docker/#custom-registry"></a>Using a custom registry<a class="anchorjs-link " href="https://jenkins.io/doc/book/pipeline/docker/#custom-registry" aria-label="Anchor link for: custom registry" data-anchorjs-icon="" style="font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; font-size: 1em; line-height: 1; font-family: anchorjs-icons; padding-left: 0.375em;"></a></h3>
<div class="paragraph">
<p>By default the <a href="https://plugins.jenkins.io/docker-workflow">Docker Pipeline</a> integrates assumes the
default Docker Registry of
<a href="https://hub.docker.com/">Docker Hub</a>.</p>
</div>
<div class="paragraph">
<p>In order to use a custom Docker Registry, users of Scripted Pipeline can wrap
steps with the <code>withRegistry()</code> method, passing in the custom Registry URL, for
example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm

    docker.withRegistry(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">https://registry.example.com</span><span style="color:#710">'</span></span>) {

        docker.image(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">my-custom-image</span><span style="color:#710">'</span></span>).inside {
            sh <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">make test</span><span style="color:#710">'</span></span>
        }
    }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>For a Docker Registry which requires authentication, add a "Username/Password"
Credentials item from the Jenkins home page and use the Credentials ID as a
second argument to <code>withRegistry()</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="CodeRay highlight nowrap"><code data-lang="groovy">node {
    checkout scm

    docker.withRegistry(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">https://registry.example.com</span><span style="color:#710">'</span></span>, <span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">'</span><span style="color:#D20">credentials-id</span><span style="color:#710">'</span></span>) {

        <span style="color:#080;font-weight:bold">def</span> customImage = docker.build(<span style="background-color:hsla(0,100%,50%,0.05)"><span style="color:#710">"</span><span style="color:#D20">my-image:</span><span style="background-color:hsla(0,0%,0%,0.07);color:black"><span style="font-weight:bold;color:#666">${</span>env.BUILD_ID<span style="font-weight:bold;color:#666">}</span></span><span style="color:#710">"</span></span>)

        <span style="color:#777">/* Push the container to the custom Registry */</span>
        customImage.push()
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr>
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="row">
<a class="prev doc-page-link" href="https://jenkins.io/doc/book/pipeline/multibranch">
⇐ Branches and Pull Requests
</a>
</div>
</div>
<div class="col-md-4">
<div class="row">
<a class="up doc-page-link" href="https://jenkins.io/doc/book/pipeline/">
⇑ Pipeline
</a>
</div>
<div class="row">
<a class="index doc-page-link" href="https://jenkins.io/doc/book/">
Index
</a>
</div>
</div>
<div class="col-md-4">
<div class="row">
<a class="next doc-page-link" href="https://jenkins.io/doc/book/pipeline/shared-libraries">
Extending with Shared Libraries ⇒
</a>
</div>
</div>
</div>
</div>
<hr>
<div class="container"></div>
<!-- starting partial _feedback-footer.html -->
<!-- HTML inclusion for chapter and section layouts (i.e. as footers) as well as
     for use in AsciiDoc file inclusions. -->

<!--
This feedback form was prepared from a Google Form, based on the techniques in
the following documentation sources:
https://codepen.io/learningcode/post/customize-a-google-form-for-your-website,
http://mrhaki.blogspot.com.au/2014/06/awesome-asciidoc-include-raw-html.html,
http://www.freshtechtips.com/2013/05/custom-google-drive-contact-form.html,
https://stackoverflow.com/questions/24436165/set-value-of-hidden-input-inside-form
-->

<!-- Feedback form functionality in JavaScript - refer to comments in this file
     for details on the functionality. -->
<script>
var feedbackForm = {
    formKey : 'e/1FAIpQLSfCEexH09x_-ytEyE7wetizqOvE_-06WkK89dpBLEJcYnOp8w'
};
$(document).ready(function() {
    window.onload = feedbackForm.start(window.location.href);
});
</script>
<script src="./Using Docker with Pipeline_files/feedback-form-functionality.js.下载"></script>

<p><a href="https://jenkins.io/doc/book/pipeline/docker/#feedback" data-toggle="collapse">Was this page helpful?</a></p>

<div id="feedback" class="collapse">

<p>Please submit your feedback about this page through this
<a href="https://jenkins.io/doc/feedback-form/">quick form</a>.</p>

<p>Alternatively, if you don't wish to complete the quick form, you can simply
indicate if you found this page helpful?</p>

<!-- Redirects to custom "thank you" page once form is submitted. -->
<script type="text/javascript">
var submitted=false;
</script>
<iframe name="hidden_iframe" id="hidden_iframe" style="display:none;" onload="if(submitted) {window.location=&#39;/doc/thank-you-for-your-feedback/&#39;;}" src="./Using Docker with Pipeline_files/saved_resource.html">
</iframe>
<form action="https://docs.google.com/forms/d/e/1FAIpQLSfCEexH09x_-ytEyE7wetizqOvE_-06WkK89dpBLEJcYnOp8w/formResponse" method="POST" id="ss-form" target="hidden_iframe" onsubmit="submitted=true;">

<!-- Set the value of the current URL into the form. -->
<input type="hidden" name="entry.1246830226" id="current-url" value="https://jenkins.io/doc/book/pipeline/docker/">

<!-- The answer bit -->
<p><input type="radio" name="entry.790002525" id="h1" value="Yes" required="">
<label for="h1">Yes</label>&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="entry.790002525" id="h2" value="No">
<label for="h2">No</label></p>

<!-- The CAPTCHA bit -->

<p><label id="ssTestLabel" for="ssTestValue">Type the answer to 8 plus 4 before clicking "Submit" below.</label><br>
<input type="text" name="ssTestValue" value="" id="ssTestValue"></p>

<!-- Submit button -->

<p><input class="button" type="submit" value="Submit"></p>

</form>

<p>See existing feedback <a href="https://docs.google.com/spreadsheets/d/1IIdpVs39JDYKg0sLQIv-MNO928qcGApAIfdW5ohfD78" target="_blank">here</a>.</p>

</div>

<!-- ending partial _feedback-footer.html -->

</div>
</div>
</div>


<script src="./Using Docker with Pipeline_files/anchor.min.js.下载"></script>
<script src="./Using Docker with Pipeline_files/tether.min.js.下载"></script>
<script src="./Using Docker with Pipeline_files/bootstrap.min.js.下载"></script>
<footer id="footer">
<div class="container">
<div class="row">
<div class="col-md-4">
<p class="box">
<a href="https://github.com/jenkins-infra/jenkins.io/edit/master/content//doc/book/pipeline/docker.adoc" title="Edit /doc/book/pipeline/docker.adoc on GitHub">
Improve this page
</a>
|
<a href="https://github.com/jenkins-infra/jenkins.io/commits/master/content//doc/book/pipeline/docker.adoc" title="View /doc/book/pipeline/docker.adoc history on GitHub">
Page history
</a>
</p>
<div class="license-box">
<div id="creativecommons">
<a href="https://creativecommons.org/licenses/by-sa/4.0/">
<p>
<img src="./Using Docker with Pipeline_files/88x31.png">
</p>
</a>
<p>
The content driving this site is licensed under the Creative
Commons Attribution-ShareAlike 4.0 license.
</p>
</div>
</div>
</div>
<div class="links col-md-8">
<div class="container">
<div class="row">
<div class="area col-md-3">
<div class="div-mar">
<h5>Resources</h5>
<ul class="resources">
<li>
<a href="https://jenkins.io/events">
Events
</a>
</li>
<li>
<a href="https://jenkins.io/doc">
Documentation
</a>
</li>
<li>
<a href="https://jenkins.io/node">
Blog
</a>
</li>
</ul>
</div>
</div>
<div class="area col-md-3">
<div class="div-mar">
<h5>Solutions</h5>
<ul>
<li>
<a href="https://jenkins.io/solutions/android">
Android
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/c">
C/C++
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/docker">
Docker
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/embedded">
Embedded
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/github">
GitHub
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/java">
Java
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/php">
PHP
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/pipeline">
Continuous Delivery
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/python">
Python
</a>
</li>
<li>
<a href="https://jenkins.io/solutions/ruby">
Ruby
</a>
</li>
</ul>
</div>
</div>
<div class="area col-md-3">
<div class="div-mar">
<h5>Project</h5>
<ul class="project">
<li>
<a href="https://issues.jenkins-ci.org/">
Issue tracker
</a>
</li>
<li>
<a href="https://wiki.jenkins.io/">
Wiki
</a>
</li>
<li>
<a href="https://github.com/jenkinsci">
GitHub
</a>
</li>
<li>
<a href="https://ci.jenkins.io/">
Jenkins on Jenkins
</a>
</li>
</ul>
</div>
</div>
<div class="area col-md-3">
<div class="div-mar">
<h5>Community</h5>
<ul class="community">
<li>
<a href="https://groups.google.com/forum/#!forum/jenkinsci-users">
Users mailing list
</a>
</li>
<li>
<a href="https://groups.google.com/forum/#!forum/jenkinsci-dev">
Developers mailing list
</a>
</li>
<li>
<a href="https://twitter.com/jenkinsci">
Twitter
</a>
</li>
<li>
<a href="https://reddit.com/r/jenkinsci">
Reddit
</a>
</li>
<li>
<a href="https://jenkins.io/merchandise">
Merchandise
</a>
</li>
<li>
<a href="https://jenkins.io/awards">
Awards
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</footer>
<script>
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
 
ga('create', 'UA-4216293-5', 'auto');
ga('send', 'pageview');
ga('set', 'anonymizeIp', true);

</script>

<script>
  !function(d,s,id) {
    var js, fjs=d.getElementsByTagName(s)[0];
    if (!d.getElementById(id)) {
      js = d.createElement(s);
      js.id=id;
      js.src="//platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js,fjs);
    }
  }(document,"script","twitter-wjs");
</script>


<iframe scrolling="no" frameborder="0" allowtransparency="true" src="./Using Docker with Pipeline_files/widget_iframe.049716a23792bca881820db70e17250b.html" title="Twitter settings iframe" style="display: none;"></iframe><iframe id="rufous-sandbox" scrolling="no" frameborder="0" allowtransparency="true" allowfullscreen="true" title="Twitter analytics iframe" style="position: absolute; visibility: hidden; display: none; width: 0px; height: 0px; padding: 0px; border: none;" src="./Using Docker with Pipeline_files/saved_resource(1).html"></iframe></body></html>